package gui;

import java.awt.EventQueue;



import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Window;

import javax.swing.SpringLayout;
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.JMenu;

import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

public class MainWindow implements ActionListener{

	private JFrame MainWindow;
	
	public int choice;
	public JButton MwbtnOk;
	public JButton MwbtnCancel;
	public JRadioButton Mwrdbtn1;
	public JRadioButton Mwrdbtn2;
	public JRadioButton Mwrdbtn3;
	
	public JMenuItem mntmVersion;
	public JMenuItem mntmNewMenuItem;
	
	public static Logger log;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.MainWindow.setVisible(true);
					log=Logger.getLogger(MainWindow.class);
					String dir=System.getProperty("user.dir");
					SimpleDateFormat form=new SimpleDateFormat("yyMMdd-h-m-s");
					String d= form.format(new Date());
					Appender app= new FileAppender(new SimpleLayout(),dir+"/test_"+d+".log");
					log.addAppender(app);
					log.log(Level.INFO, "testLog1");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		MainWindow = new JFrame();
		MainWindow.setTitle("Application");
		MainWindow.setBounds(100, 100, 450, 250);
		MainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.choice=0;
		
		JPanel panel = new JPanel();
		GroupLayout groupLayout = new GroupLayout(MainWindow.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
		);
		
		this.MwbtnOk = new JButton("OK");
		
		this.MwbtnCancel = new JButton("Cancel");
		
		JLabel lblWhichTransformationDo = new JLabel("Which transformation do you want to perform?");
		
		this.Mwrdbtn1 = new JRadioButton("From Express file (.spf) to Express Xml file");
		this.Mwrdbtn1.setMnemonic(KeyEvent.VK_A);
		
		this.Mwrdbtn2 = new JRadioButton("From Express file (.spf) to AltaRica Xml file");
		this.Mwrdbtn1.setMnemonic(KeyEvent.VK_B);
		
		this.Mwrdbtn3 = new JRadioButton("From Express Xml file to AltaRica Xml file");
		this.Mwrdbtn1.setMnemonic(KeyEvent.VK_C);
		
		//Group the radio button
		//le button groupe sert à que si un bouton est selectionné les autres 
		//seront annulés
		ButtonGroup group = new ButtonGroup();
		group.add(this.Mwrdbtn3);
		group.add(this.Mwrdbtn2);
		group.add(this.Mwrdbtn1);
		
		//register a listener for the radio buttons
		this.Mwrdbtn1.addActionListener(this);
		this.Mwrdbtn2.addActionListener(this);
		this.Mwrdbtn3.addActionListener(this);
		
		//register a listener for the ok button
		this.MwbtnOk.addActionListener(this);
		this.MwbtnCancel.addActionListener(this);
		
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap(268, Short.MAX_VALUE)
					.addComponent(this.MwbtnOk)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(this.MwbtnCancel)
					.addGap(27))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(47)
					.addComponent(lblWhichTransformationDo)
					.addContainerGap(63, Short.MAX_VALUE))
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(70)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(this.Mwrdbtn3)
						.addComponent(this.Mwrdbtn2)
						.addComponent(this.Mwrdbtn1))
					.addContainerGap(51, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(31)
					.addComponent(lblWhichTransformationDo)
					.addGap(18)
					.addComponent(this.Mwrdbtn1)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(this.Mwrdbtn2)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(this.Mwrdbtn3)
					.addPreferredGap(ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(this.MwbtnOk)
						.addComponent(this.MwbtnCancel))
					.addContainerGap())
		);
		
		panel.setLayout(gl_panel);
		MainWindow.getContentPane().setLayout(groupLayout);
		
		JMenuBar menuBar = new JMenuBar();
		MainWindow.setJMenuBar(menuBar);
		
		JMenu mnInformation = new JMenu("Information");
		menuBar.add(mnInformation);
		
		this.mntmVersion = new JMenuItem("Version");
		mnInformation.add(mntmVersion);
		this.mntmVersion.addActionListener(this);
		
		this.mntmNewMenuItem = new JMenuItem("Help");
		menuBar.add(mntmNewMenuItem);
		this.mntmNewMenuItem.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e){
		Object source = e.getSource();
		if (source==this.Mwrdbtn1)
			this.choice=1;
		if (source==this.Mwrdbtn2)
			this.choice=2;
		if(source==this.Mwrdbtn3)
			this.choice=3;
		if(source==this.MwbtnOk){
			System.out.println("bouton ok = "+this.choice);
			if (this.choice==1){
				this.MainWindow.setVisible(false);
				this.MainWindow.dispose();
				Window1.main(null);
			}else if(this.choice==2){
				this.MainWindow.setVisible(false);
				this.MainWindow.dispose();
				Window3.main(null);
			}else if (this.choice==3){
				this.MainWindow.setVisible(false);
				this.MainWindow.dispose();
				Window2.main(null);
			}else{
				Icon ic= new ImageIcon("/home/ubuntu/Bureau/makechoice.jpg");
				JOptionPane.showMessageDialog(null,"You have to make a choice", "ERROR", JOptionPane.ERROR_MESSAGE,ic);
			}
		}
		if(source==this.MwbtnCancel){
			this.MainWindow.dispose();
		}
		if(source==this.mntmVersion){
			
			Version.main(null);
		}
		if(source==this.mntmNewMenuItem){
			Help.main(null);
		}
	}
	
	
}
